package com.gxj.study.shopdemo.test;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.gxj.study.shopdemo.R;
import com.gxj.study.shopdemo.adapter.decoration.DividerItemDecortion;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 祥杰 on 2016/8/5.
 */

public class SwipeRefresh extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private SwipeRefreshLayout mLayout;

    private List<String> datas = new ArrayList<>(0);

    private MyAdapter myAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swiperefresh);
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mLayout = (SwipeRefreshLayout) findViewById(R.id.refreshLayout);

        initDatas();
        initRecyclerView();
        initRefresh();
    }


    private void initRecyclerView() {
        myAdapter = new MyAdapter(datas);
        mRecyclerView.setAdapter(myAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mRecyclerView.addItemDecoration(new DividerItemDecortion(this, DividerItemDecortion.VERTICAL_LIST));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        myAdapter.setListener(new MyAdapter.OnItemClickListener() {
            @Override
            public void onClick(View v, int position, String city) {
                Toast.makeText(SwipeRefresh.this, "city:" + city + ",position:" + position, Toast.LENGTH_LONG).show();
            }
        });
    }

    private void initDatas() {
        datas.add("New York");
        datas.add("Boston");
        datas.add("Washington");
        datas.add("San Francisco");
        datas.add("California");
        datas.add("Chicago");
        datas.add("Houston");
        datas.add("Phoenix");
        datas.add("Philadelphia");
        datas.add("Pennsylvania");
        datas.add("San Antonio");
        datas.add("Austin");
        datas.add("Milwaukee");
        datas.add("Las Vegas");
        datas.add("Oklahoma");
        datas.add("Portland");
        datas.add("Mexico");
    }

    private void initRefresh() {
        mLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        mLayout.setDistanceToTriggerSync(100);
        mLayout.setSize(SwipeRefreshLayout.LARGE);
        mLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i <= 10; i++) {
                            myAdapter.addData(i, "new City:" + i);
                        }
                        myAdapter.notifyItemChange(0, 10);
                        mRecyclerView.scrollToPosition(0);
                        mLayout.setRefreshing(false);
                    }
                }, 3000);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}














